package com.chenyun.system.controller;

import cn.dev33.satoken.annotation.SaCheckPermission;
import cn.hutool.core.collection.CollectionUtil;
import com.chenyun.common.annotation.Log;
import com.chenyun.common.enums.operlog.BusinessTypeEnum;
import com.chenyun.system.model.SysDictData;
import com.chenyun.system.service.ISysDictDataService;
import com.chenyun.system.service.ISysDictTypeService;
import com.chenyun.boot.web.controller.BaseController;
import com.chenyun.common.vo.AjaxResult;
import com.chenyun.common.vo.PageResult;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 数据字典信息
 * 
 * @author ruoyi
 */
@RestController
@RequestMapping("/dict/data")
@Api(tags = "数据字典-admin")
public class SysDictDataController extends BaseController<ISysDictDataService, SysDictData>
{
    @Autowired
    private ISysDictDataService dictDataService;
    
    @Autowired
    private ISysDictTypeService dictTypeService;

    @ApiOperation("获取字典列表")
    @GetMapping("/list")
    @SaCheckPermission("system:dict:data:list")
    public PageResult list(SysDictData dictData)
    {
//        startPage();
        List<SysDictData> list = dictDataService.selectDictDataList(dictData);
        return getDataTable(list);
    }


    /**
     * 查询字典数据详细
     */
    @ApiOperation("查询字典数据详细")
    @GetMapping(value = "/{dictCode}")
    @SaCheckPermission("system:dict:data:info")
    public AjaxResult getInfo(@PathVariable Long dictCode)
    {
        return AjaxResult.SUCCESS(dictDataService.selectDictDataById(dictCode));
    }

    /**
     * 根据字典类型查询字典数据信息
     */
    @ApiOperation("根据字典类型查询字典数据信息")
    @GetMapping(value = "/type/{dictType}")
    public AjaxResult dictType(@PathVariable String dictType)
    {
        List<SysDictData> data = dictTypeService.selectDictDataByType(dictType);
        if (CollectionUtil.isEmpty(data))
        {
            data = new ArrayList<SysDictData>();
        }
        return AjaxResult.SUCCESS(data);
    }

    @ApiOperation("根据多个字典类型6查询字典数据信息")
    @PostMapping(value = "/type/dictTypes")
    public AjaxResult dictTypes(@RequestBody String[] types)
    {
        if (types==null||types.length<=0){
            return AjaxResult.FAIL("字典类型无效");
        }
        Map<String ,List<SysDictData>> listMap=new HashMap<>(types.length);
        for (String type : types) {
            List<SysDictData> data = dictTypeService.selectDictDataByType(type);
            if (CollectionUtil.isEmpty(data))
            {
                data = new ArrayList<SysDictData>();
            }
            listMap.put(type,data);
        }
        return AjaxResult.SUCCESS(listMap);
    }




    /**
     * 新增字典类型
     */
    @ApiOperation("新增字典类型")
    @PostMapping
    @SaCheckPermission("system:dict:data:add")
    @Log(title = "新增数据字典",businessType = BusinessTypeEnum.INSERT)
    public AjaxResult add(@Validated @RequestBody SysDictData dict)
    {
//        dict.setCreateBy(SecurityUtils.getUsername());
        return toAjax(dictDataService.insertDictData(dict));
    }

    /**
     * 修改保存字典类型
     */
    @ApiOperation("修改保存字典类型")
    @PostMapping("edit")
    @SaCheckPermission("system:dict:data:edit")
    @Log(title = "修改数据字典",businessType = BusinessTypeEnum.INSERT)
    public AjaxResult edit(@Validated @RequestBody SysDictData dict)
    {
//        dict.setUpdateBy(SecurityUtils.getUsername());
        return toAjax(dictDataService.updateDictData(dict));
    }

    /**
     * 删除字典类型
     */
    @ApiOperation("删除字典类型")
    @PostMapping("del")
    @SaCheckPermission("system:dict:data:del")
    @Log(title = "删除数据字典",businessType = BusinessTypeEnum.INSERT)
    public AjaxResult remove(@RequestBody Long[] dictCodes)
    {
        dictDataService.deleteDictDataByIds(dictCodes);
        return success();
    }
}
